<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 12-8-29
 * Time: 下午1:56
 * To change this template use File | Settings | File Templates.
 */
class mod_category extends MY_Model
{

    private $category = array();

    private $tree = array();
    /**
     * 获取电影列表
     *
     * @param string $field
     * @param int $limit
     * @param int $offset
     * @param null $where
     * @param null $order
     * @return mixed
     */

    public function __construct()
    {
        parent::__construct();
        $this->init();
    }

    private function init()
    {
        $this->category = $this->db->select('*')->from('category')->order_by('id ASC, pid ASC')->get()->result_array('id');
    }

    public function get_category($tree = false)
    {
         if($tree)
         {
             $this->tree = $this->category;
             foreach($this->tree as $id => $item)
             {
                 if($item['pid'] > 0)
                 {
                     $this->tree[$item['pid']]['children'][$id] = $item;
                     unset($this->tree[$id]);
                 }
             }
             return $this->tree;
         }
         return $this->category;
    }


    public function getMovieList($field = '*', $limit = 20, $offset = 0, $where = null, $order = null)
    {
        $this->db->select($field)->from('movie');
        $this->db->where('status', '1');
        $where && $this->db->where($where);
        $order && $this->db->order_by($order);
        $this->db->limit($limit, $offset);

        return $this->db->get()->result_array();
    }

    /**
     * 获取电影数量
     *
     * @param null $where
     * @return mixed
     */
    public function getMovieCount($where = null)
    {
        $this->db->select('*')->from('movie');
        $this->db->where('status', '1');
        $where && $this->db->where($where);

        return $this->db->count_all_results();
    }

    /**
     * 获取电影
     *
     * @param $mId
     * @param int $limit
     * @param int $offset
     * @param string $field
     * @param null $where
     * @param null $order
     * @return mixed
     */
    public function getMovie($mId, $limit = 20, $offset = 0, $field= "*", $where = null, $order = null)
    {
        $this->db->select($field)->from('movie');
        $this->db->where('status', '1');
        $where && $this->db->where($where);
        $order && $this->db->order_by($order);
        $this->db->limit($limit, $offset);

        if(is_array($mId))
        {
            return  $this->db->where_in('id', $mId)->get()->result_array();
        }
        return $this->db->where('id', $mId)->get()->row_array();
    }

    /**
     * 获取资源下载地址
     *
     * @param $mId
     * @param int $limit
     * @param int $offset
     * @param string $field
     * @param null $where
     * @param null $order
     * @return mixed
     */
    public function getDownLoad($mId, $limit = 20, $offset = 0, $field= "*", $where = null, $order = null)
    {
        $this->db->select($field)->from('download_list');
        $where && $this->db->where($where);
        $order && $this->db->order_by($order);
        $this->db->limit($limit, $offset);

        return  $this->db->where_in('video_id', $mId)->get()->result_array();
    }
}
